#!/usr/bin/env python

# Copyright (c) 2007 Marcelo Lira dos Santos
#
# Author: Marcelo Lira dos Santos <setanta@gmail.com>
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation; either version 2 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
# USA

import os
import pygame
from pygame.locals import *
import config

class Tape:

    FG = 0
    BG = 1

    def __init__(self, surface, cell_width):
        self.surface = surface
        self.cell_width = cell_width
        self.cell_height = cell_width
        self.color = [(0, 0, 0), (255, 255, 255)]
        self.base_pos = (0, 0)
        width, height = surface.get_size()

        self.font = pygame.font.Font(config.font_path, 40)
        texture = pygame.image.load(os.path.join(config.data_dir,\
                    'tape.png')).convert_alpha()
        
        texture_width = texture.get_width()
        skin_height = texture.get_height()
        tiles = surface.get_width() / texture_width + 2 
        self.skin = pygame.Surface((tiles * texture_width, \
                skin_height))
        self.skin = self.skin.convert(self.skin.get_bitsize(), SRCALPHA)

        for i in range(tiles):
            self.skin.blit(texture, (18 * i, 0))

        self.base_skin_y = (self.cell_height - skin_height) / 2

        #FIXME: Tape deve receber o conjunto dos simbolos aceitos pela
        #   maquina de turing e guardar a renderizacao de cada um
        #   dos simbolos num dicionario.
        self.tape_alphabet = {}

    def get_base_position(self):
        return self.base_pos

    def set_tape_alphabet(self, alphabet):
        for symbol in alphabet:
            if symbol == ' ':
                symbol='_'
            self.tape_alphabet[symbol] = \
                    self.font.render(symbol, \
                    True, self.color[Tape.FG])

    def set_base_position(self, pos=(0,0)):
        self.base_pos = pos

    def draw(self, string, head):
        width = self.surface.get_width()
        border = 10

        displacement = - (head * self.cell_width)
        base_x = self.base_pos[0] + displacement

        min = 0
        max = ((width - border - base_x) / self.cell_width) + 1
        if max > len(string):
            suffix = [' '] * (max - len(string))
            string = string + suffix

        tape_width = len(string[min:max]) * self.cell_width

        #FIXME: rectangles can be moved
        #tape = pygame.Rect(base_x, self.base_pos[1], tape_width, 50)
        #pygame.draw.rect(self.surface, self.color[Tape.FG], tape, 1)
        tape_x = self.base_pos[0] + displacement
        self.surface.blit(self.skin, \
                (tape_x, self.base_pos[1] + self.base_skin_y))
        
        if tape_x + self.skin.get_width() < self.surface.get_width():
            tape_x += self.skin.get_width()
            self.surface.blit(self.skin, \
                    (tape_x, self.base_pos[1] + self.base_skin_y))

        x = 0
        for i in xrange(min, max):
            symbol = string[i]
            if symbol == ' ':
                symbol = '_'
            sym = self.tape_alphabet[symbol]
            base_x = self.base_pos[0] + displacement + x
            sym_x = base_x + ((self.cell_width - sym.get_width()) / 2)
            sym_y = self.base_pos[1] + ((self.cell_height - sym.get_height()) / 2)
            x += self.cell_width

            self.surface.blit(sym, (sym_x, sym_y))

